﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 用户体验师仓储
/// </summary>
public class MdUserExperiencerRep : BaseRep<MdUserExperiencerModel>
{
    private static string sqlFields = string.Format(@"
        `user_experiencer_id`,
        `name`,
        `sex`,
        `birthday`,
        `phone`,
        `email`,
        `state`,
        `city`,
        `province`,
        `region`,
        `country`,
        `zip_code`,
        `address`,
        `scopes`,
        `influence`,
        `works`,
        `user_id`,
        `display`,
        `status`,
        `del_flag`,
        `reason`,
        `create_by`,
        `create_time`,
        `update_by`,
        `update_time`,
        `remark`
    ");

    /// <summary>
    /// 通过编号获取体验师详情
    /// </summary>
    /// <param name="id">产品编号</param>
    /// <returns>MdUserExperiencerModel</returns>
    public MdUserExperiencerModel? GetMdUserExperiencerById(long id)
    {
        string sqlWhere = string.Format(@" and `del_flag` = '0' and `user_experiencer_id` = @UserExperiencerId ");

        return QuerySingle(sqlFields, sqlWhere, new { UserExperiencerId = id });
    }

    /// <summary>
    /// 获取体验师列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒叙</param>
    /// <returns>IEnumerable<MdUserExperiencerModel></returns>
    public IEnumerable<MdUserExperiencerModel> GetMdUserExperiencerList(MdUserExperiencerModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.Name))
        {
            sqlWhere += " and `name` like concat('%', @Name, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Sex))
        {
            sqlWhere += " and `sex` = @Sex ";
        }
        if(!string.IsNullOrWhiteSpace(model.Phone))
        {
            sqlWhere += " and `phone` = @Phone ";
        }
        if(!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlWhere += " and `email` = @Email ";
        }
        if(!string.IsNullOrWhiteSpace(model.State))
        {
            sqlWhere += " and `state` = @State ";
        }
        if (!string.IsNullOrWhiteSpace(model.City))
        {
            sqlWhere += " and `city` = @City ";
        }
        if (!string.IsNullOrWhiteSpace(model.Province))
        {
            sqlWhere += " and `province` = @Province ";
        }
        if (!string.IsNullOrWhiteSpace(model.Region))
        {
            sqlWhere += " and `region` = @Region ";
        }
        if (!string.IsNullOrWhiteSpace(model.Country))
        {
            sqlWhere += " and `country` = @Country ";
        }
        if(!string.IsNullOrWhiteSpace(model.ZipCode))
        {
            sqlWhere += " and `zip_code` = @ZipCode ";
        }
        if (model.UserId > 0)
        {
            sqlWhere += " and `user_id` = @UserId ";
        }
        if(!string.IsNullOrWhiteSpace(model.Display))
        {
            sqlWhere += " and `display` = @Display ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取体验师列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<MdUserExperiencerModel></returns>
    public Page<MdUserExperiencerModel> GetMdUserExperiencerPageList(MdUserExperiencerModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.Name))
        {
            sqlWhere += " and `name` like concat('%', @Name, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Sex))
        {
            sqlWhere += " and `sex` = @Sex ";
        }
        if (!string.IsNullOrWhiteSpace(model.Phone))
        {
            sqlWhere += " and `phone` = @Phone ";
        }
        if (!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlWhere += " and `email` = @Email ";
        }
        if (!string.IsNullOrWhiteSpace(model.State))
        {
            sqlWhere += " and `state` = @State ";
        }
        if (!string.IsNullOrWhiteSpace(model.City))
        {
            sqlWhere += " and `city` = @City ";
        }
        if (!string.IsNullOrWhiteSpace(model.Province))
        {
            sqlWhere += " and `province` = @Province ";
        }
        if (!string.IsNullOrWhiteSpace(model.Region))
        {
            sqlWhere += " and `region` = @Region ";
        }
        if (!string.IsNullOrWhiteSpace(model.Country))
        {
            sqlWhere += " and `country` = @Country ";
        }
        if (!string.IsNullOrWhiteSpace(model.ZipCode))
        {
            sqlWhere += " and `zip_code` = @ZipCode ";
        }
        if (model.UserId > 0)
        {
            sqlWhere += " and `user_id` = @UserId ";
        }
        if (!string.IsNullOrWhiteSpace(model.Display))
        {
            sqlWhere += " and `display` = @Display ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过编号更新体验师
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>long</returns>
    public long UpdateMdUserExperiencerById(MdUserExperiencerModel model)
    {
        var sqlSet = string.Empty;

        #region ===set===
        if (model.Name != null)
        {
            sqlSet += " `name` = @Name, ";
        }

        if (model.Sex != null)
        {
            sqlSet += " `sex` = @Sex, ";
        }

        if (model.Birthday.HasValue)
        {
            sqlSet += " `birthday` = @Birthday, ";
        }

        if (model.Phone != null)
        {
            sqlSet += " `phone` = @Phone, ";
        }

        if (model.Email != null)
        {
            sqlSet += " `email` = @Email, ";
        }

        if (model.State != null)
        {
            sqlSet += " `state` = @State, ";
        }

        if (model.City != null)
        {
            sqlSet += " `city` = @City, ";
        }

        if (model.Province != null)
        {
            sqlSet += " `province` = @Province, ";
        }

        if (model.Region != null)
        {
            sqlSet += " `region` = @Region, ";
        }

        if (model.Country != null)
        {
            sqlSet += " `country` = @Country, ";
        }

        if (model.ZipCode != null)
        {
            sqlSet = " `zip_code` = @ZipCode, ";
        }

        if (model.Address != null)
        {
            sqlSet = " `address` = @Address, ";
        }

        if (model.Scopes != null)
        {
            sqlSet += " `scopes` = @Scopes, ";
        }

        if (model.Influence != null)
        {
            sqlSet += " `influence` = @Influence, ";
        }

        if (model.Works != null)
        {
            sqlSet += " `works` = @Works, ";
        }

        if (model.UserId > 0)
        {
            sqlSet += " `user_id` = @UserId, ";
        }

        if (model.Reason != null)
        {
            sqlSet += " `reason` = @Reason, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Display))
        {
            sqlSet += " `display` = @Display, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (model.Remark != null)
        {
            sqlSet += " `remark` = @Remark, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `del_flag` = '0' and `user_experiencer_id` = @UserExperiencerId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过编号删除体验师
    /// </summary>
    /// <param name="ids">编号多个逗号分隔</param>
    /// <returns>int</returns>
    public int DeleteMdUserExperiencerById(string ids)
    {
        string sqlWhere = " and `user_experiencer_id` in @UserExperiencerIds ";

        return Delete(sqlWhere, new { UserExperiencerIds = ids.Split(',').ToArray() });
    }

    /// <summary>
    /// 检查当前用户是否申请过体验师
    /// </summary>
    /// <param name="userId">用户编号</param>
    /// <returns>MdUserExperiencerModel?</returns>
    public MdUserExperiencerModel? CheckMdUserExperiencer(long userId)
    {
        string sqlWhere = " and `del_flag` = '0' and `user_id` = @UserId ";

        return QuerySingle(sqlFields, sqlWhere, new { UserId = userId });
    }
}

